#include "AnimationSprite.h"
#include "Animation.h"
#include "Utils.h"

AnimationSprite::AnimationSprite()
{
	curAnimation = 0;
}

void AnimationSprite::AddAnimation(Animation* ani)
{
	animations.push_back(ani);
	if(curAnimation == 0)
		curAnimation = *(animations.begin());
}

void AnimationSprite::RemoveAnimation(int id)
{
	int indexNeedDelete;
	int animationSize = animations.size();
	for(int i = 0; i < animationSize; i++)
	{
		Animation* ani = animations[i];
		if(ani->GetID()==id)
		{
			indexNeedDelete = i;
			break;
		}
	}
	animations.erase(animations.begin() + indexNeedDelete);
	if(curAnimation == 0)
		curAnimation = *(animations.begin());
}

Animation* AnimationSprite::SetCurAnimation(int id)
{
	int animationsSize = animations.size();
	for(int i  = 0; i < animationsSize; i++)
	{
		Animation* ani = animations[i];
		if(ani->GetID()==id)
		{
			curAnimation = ani;
			break;
		}
	}
	return curAnimation;
}

Animation* AnimationSprite::GetAnimation(int id)
{
	int animationsSize = animations.size();
	for(int i  = 0; i < animationsSize; i++)
	{
		Animation* ani = animations[i];
		if(ani->GetID()==id)
		{
			return ani;
		}
	}
}
bool AnimationSprite::Update()
{
	return curAnimation->Update();
}

bool AnimationSprite::Update(int aniID)
{
	SetCurAnimation(aniID);
	return Update();
}

void AnimationSprite::Render(int x, int y, float deep)
{
	curAnimation->Render(x, y, deep);
}

void AnimationSprite::Render(int x, int y, float alpha, float deep)
{
	curAnimation->Render(x, y, alpha, deep);
}

void AnimationSprite::Render(int x, int y, float xScale, float yScale, float deep)
{
	curAnimation->Render(x, y, xScale, yScale, 0.0f, 1.0f, deep);
}

void AnimationSprite::Render(int x, int y, float xScale, float yScale, float angle, float alpha,float deep)
{
	curAnimation->Render(x, y, xScale, yScale, angle, alpha, deep);
}

void AnimationSprite::Render(int aniID, int x, int y, float xScale, float yScale, float angle, float alpha,float deep)
{
	SetCurAnimation(aniID);
	curAnimation->Render(x, y, xScale, yScale, angle, alpha, deep);
}



AnimationSprite::~AnimationSprite()
{
	int animationsSize = animations.size();
	for (int i = 0; i < animationsSize; i++)
	{
		DELETE_SAFELY(animations[i]);
	}
	animations.clear();
}